﻿using CommunityToolkit.Mvvm.ComponentModel;
using System.Reflection;
using Wpf.Ui.Appearance;
using Wpf.Ui.Controls;
using Wpf.Ui;
using System.Windows.Media;
using Wpf.Ui.Extensions;
using System;

namespace XC.WpfHost.ViewModels
{
    /// <summary>
    /// 设置界面,INavigationAware
    /// </summary>
    public sealed partial class SettingsViewModel : ObservableObject, INavigationAware
    {
        private readonly INavigationService _navigationService;

        private bool _isInitialized = false;

        [ObservableProperty]
        private string _appVersion = String.Empty;

        [ObservableProperty]
        private ApplicationTheme _currentApplicationTheme = ApplicationTheme.Unknown;

        [ObservableProperty]
        private NavigationViewPaneDisplayMode _currentApplicationNavigationStyle =
            NavigationViewPaneDisplayMode.Left;

        public SettingsViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;
        }

        public void OnNavigatedTo()
        {
            if (!_isInitialized)
            {
                InitializeViewModel();
            }
        }

        public void OnNavigatedFrom() { }

        partial void OnCurrentApplicationThemeChanged(ApplicationTheme oldValue, ApplicationTheme newValue)
        {
            ApplicationThemeManager.Apply(newValue);
        }

        partial void OnCurrentApplicationNavigationStyleChanged(
            NavigationViewPaneDisplayMode oldValue,
            NavigationViewPaneDisplayMode newValue
        )
        {
            _ = _navigationService.SetPaneDisplayMode(newValue);
        }

        private void InitializeViewModel()
        {
            CurrentApplicationTheme = ApplicationThemeManager.GetAppTheme();
            AppVersion = $"{GetAssemblyVersion()}";

            ApplicationThemeManager.Changed += OnThemeChanged;

            _isInitialized = true;
        }

        private void OnThemeChanged(ApplicationTheme currentApplicationTheme, Color systemAccent)
        {
            // Update the theme if it has been changed elsewhere than in the settings.
            if (CurrentApplicationTheme != currentApplicationTheme)
            {
                CurrentApplicationTheme = currentApplicationTheme;
            }
        }

        private string GetAssemblyVersion()
        {
            return Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? String.Empty;
        }
    }

}
